package com.aisile.shop.controller;

import com.aisile.pojo.TbSeller;
import com.aisile.sellergoods.service.SellerService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("UserServiceImpl")
public class UserServiceImpl  implements UserDetailsService{

    @Reference
    SellerService sellerService;

   //setter 构造器 注解注入



    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        List<GrantedAuthority> grantedList=new ArrayList<>();

        //去数据库 查询对应的用户
        grantedList.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        TbSeller seller = sellerService.findById(username);

        if(seller!=null){

            if("1".equals(seller.getStatus())){

                return new User(username,seller.getPassword(),grantedList);
            }
        }
        return null;


    }
}
